package com.minisoft.ai;// 建议dashscope SDK的版本 >= 2.15.0

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.dashscope.app.Application;
import com.alibaba.dashscope.app.ApplicationParam;
import com.alibaba.dashscope.app.ApplicationResult;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import io.reactivex.Flowable;
import lombok.extern.slf4j.Slf4j;

// 智能体应用调用实现流式输出结果
@Slf4j
public class Main2 {
    public static void streamCall() throws NoApiKeyException, InputRequiredException {
        ApplicationParam param = ApplicationParam.builder()
                // 若没有配置环境变量，可用百炼API Key将下行替换为：.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中，以减少API Key泄露风险。
                .apiKey("sk-837018ca105a4629ac23b8545d3c844a")
                .appId("64a1c6a0457340fdbd58de840e105c79")
                .prompt("我比较看重学区质量，华发有哪些盘比较合适")
//                .parameter("flow_stream_mode","agent_format")
//                .parameter("flow_stream_mode", "full_thoughts")
                .incrementalOutput(true)// 增量输出
                .hasThoughts(true) // 工作流应用实现流式输出需要设置此参数为true，输出结果在thoughts字段中查看
                .build();
        Application application = new Application();
        // .streamCall（）：流式输出内容
        log.info("开始调用智能体应用");
        Flowable<ApplicationResult> result = application.streamCall(param);
        result.blockingForEach(data -> {
            if (data.getOutput().getThoughts() != null) {
                data.getOutput().getThoughts().forEach(thought -> {
                    JSONObject response = JSONUtil.parseObj(thought.getResponse());
//                    String nodeType = response.getStr("nodeType");
                    ThreadUtil.sleep(100);
                    System.out.printf("%s\n",data.getOutput().getThoughts());// 处理输出只展示thoughts字段
//                        System.out.printf("%s\n",thought);// 处理输出只展示thoughts字段
                    JSONObject nodeResult = response.getJSONObject("nodeResult");
                    if (nodeResult != null) {
                        String reasoningContent = nodeResult.getStr("reasoningContent");
                        if (StrUtil.isNotBlank(reasoningContent)) {
                            log.info("推理过程：{}", reasoningContent);
                        }
                    }
                });
            }
        });
    }

    public static void main(String[] args) {
        try {
            streamCall();
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            System.out.printf("Exception: %s", e.getMessage());
            System.out.println("请参考文档：https://help.aliyun.com/zh/model-studio/developer-reference/error-code");
        }
        System.exit(0);
    }
}